package com.yougou.utils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yougou.data.model.Order;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class OrderManager {
    
    private static final String KEY_ORDERS_PREFIX = "key_orders_";
    private static List<Order> orders;
    private static Gson gson = new Gson();
    
    
    private static String getOrdersKey() {
        String uid = UserManager.isLogin() ? UserManager.getCurrentUser().getUid() : "guest";
        return KEY_ORDERS_PREFIX + uid;
    }
    
    
    public static Order createOrder(String goodsName, double price, int quantity, String imageUrl,
                                   String receiverName, String receiverPhone, String receiverAddress) {
        Order order = new Order();
        
        // 生成订单号
        String orderNumber = "YG" + System.currentTimeMillis();
        order.setOrderNumber(orderNumber);
        order.setOrderId(orderNumber);
        
        // 设置订单信息
        order.setOrderPrice(price * quantity);
        order.setOrderStatus(1); // 1-待付款
        order.setCreateTime(System.currentTimeMillis());
        order.setConsigneeAddr(receiverAddress);
        
        // 设置收货人信息
        order.setReceiverName(receiverName);
        order.setReceiverPhone(receiverPhone);
        order.setReceiverAddress(receiverAddress);
        
        // 创建商品列表
        List<Order.OrderGoods> goodsList = new ArrayList<>();
        Order.OrderGoods goods = new Order.OrderGoods();
        goods.setGoodsName(goodsName);
        goods.setGoodsPrice(price);
        goods.setGoodsNumber(quantity);
        goods.setGoodsSmallLogo(imageUrl);
        goodsList.add(goods);
        order.setGoodsList(goodsList);
        
        // 保存订单
        addOrder(order);
        
        return order;
    }
    
    
    public static void addOrder(Order order) {
        List<Order> orderList = getOrders();
        orderList.add(0, order); // 添加到列表开头
        saveOrders(orderList);
    }
    
    
    public static List<Order> getOrders() {
        // 每次都重新加载，确保数据是当前用户的
        String json = SPUtils.getString(getOrdersKey());
        if (json.isEmpty()) {
            orders = new ArrayList<>();
        } else {
            Type type = new TypeToken<List<Order>>() {}.getType();
            orders = gson.fromJson(json, type);
            if (orders == null) {
                orders = new ArrayList<>();
            }
        }
        return orders;
    }
    
    
    public static List<Order> getOrdersByStatus(int status) {
        List<Order> allOrders = getOrders();
        if (status == 0) {
            return allOrders;
        }
        
        List<Order> filteredOrders = new ArrayList<>();
        for (Order order : allOrders) {
            if (order.getOrderStatus() == status) {
                filteredOrders.add(order);
            }
        }
        return filteredOrders;
    }
    
    
    public static Order getOrderByNumber(String orderNumber) {
        List<Order> orderList = getOrders();
        for (Order order : orderList) {
            if (order.getOrderNumber().equals(orderNumber)) {
                return order;
            }
        }
        return null;
    }
    
    
    public static void updateOrder(Order order) {
        List<Order> orderList = getOrders();
        for (int i = 0; i < orderList.size(); i++) {
            if (orderList.get(i).getOrderNumber().equals(order.getOrderNumber())) {
                orderList.set(i, order);
                saveOrders(orderList);
                return;
            }
        }
    }
    
    
    public static void deleteOrder(String orderNumber) {
        List<Order> orderList = getOrders();
        orderList.removeIf(order -> order.getOrderNumber().equals(orderNumber));
        saveOrders(orderList);
    }
    
    
    public static void clearOrders() {
        orders = new ArrayList<>();
        saveOrders(orders);
    }
    
    
    private static void saveOrders(List<Order> orderList) {
        orders = orderList;
        String json = gson.toJson(orderList);
        SPUtils.putString(getOrdersKey(), json);
    }
}

